<?php


/**
 * \file luminous_grammars.class.php
 * \brief Grammar lookup table definition.
 */ 
/**
 * \class LuminousGrammars
 * \author Mark Watkinson
 * \brief A glorified lookup table for languages to grammars.
 * One of these is instantiated in the global scope at the bottom of this source.
 * The parser assumes it to exist and uses it to look up grammars.
 * Users seeking to override grammars or add new grammars should add their
 * grammar into '$luminous_grammars'.
 *
 */

class LuminousGrammars
{
  private $lookup_table = array(); /**< The language=>grammar lookup table*/
  private $default_grammar = null; /**< Default grammar to use if none is found
    for a particular language */

  private $descriptions = array();

  /**
   * Adds a grammar into the table, or overwrites an existing grammar.
   *
   * \param language_name may be either a string or an array of strings, if
   *    multiple languages are to use the same grammar
   * \param $grammar the name of the LuminousGrammar object as string, (not an 
   * actual instance!)
   * \param lang_description a human-readable descriptio of the language.
   *
   */
  public function AddGrammar($language_name, $grammar, 
    $lang_description)
  {

    
    if (is_array($language_name))
    {
      foreach($language_name as $l)
      { 
        $this->lookup_table[$l] = $grammar;
        $this->AddDescription($lang_description, $l);
      }
    }
    else
    {      
      $this->lookup_table[$language_name] = $grammar;
      $this->AddDescription($lang_description, $language_name);
    }
  }
  
  private function AddDescription($language_name, $language_code)
  {
    if (!isset($this->descriptions[$language_name]))
      $this->descriptions[$language_name] = array();
    $this->descriptions[$language_name][] = $language_code;
  }
  
  
  private function UnsetDescription($language_name)
  {
    foreach($this->descriptions as &$d)
    {
      foreach($d as $k=>$l)
      {
        if($l === $language_name)
          unset($d[$k]);
      }
    }    
  }

  /**
   * Removes a grammar from the table
   *
   * \param language_name may be either a string or an array of strings, each of
   *    which will be removed from the lookup table.
   */
  public function RemoveGrammar($language_name)
  {
    if (is_array($language_name))
    {
      foreach($language_name as $l)
      {
        unset($this->lookup_table[$l]);
        $this->UnsetDescription($l);
      }
    }
    else
    {
      $this->UnsetDescription($language_name);
      unset($this->lookup_table[$language_name]);
    }
  }

  /**
   * Sets the default grammar. This is used when none matches a lookup
   * \param grammar the LuminousGrammar object
   */
  public function SetDefaultGrammar($grammar)
  {
    $this->default_grammar = $grammar;
  }

  /**
   * Returns a grammar for a language
   * \param language_name the name under which the gramar was originally indexed
   * \param default if true: if the grammar doesn't exist, return the default
   *    grammar. If false, return false
   * \return The grammar, the default grammar, or false.
   */
  function GetGrammar($language_name, $default=true)
  {
    if (array_key_exists($language_name, $this->lookup_table))
      return new $this->lookup_table[$language_name];
    elseif($this->default_grammar !== null && $default === true)
      return new $this->default_grammar;

    return false;
  }
  
  function ListGrammars()
  {
    $l = $this->descriptions;    
    return $l;
  }
  
}
?>